$(function(){
	document.oncontextmenu = function(e){
        e.preventDefault();
    };
	$('#backModel').click(function(){
		window.location.href=webroot + '/dep/list.do';
	});
	
	startInit();
	
function startInit(){
	$.ajax({
		url:webroot + '/dep/organize.do',
		type:'post',
		success:function(rep){
			console.log(rep);
			var showlist = $("<ul id='org' style='display:none'></ul>");  
            showall(JSON.parse(rep), showlist);  
            $("#jOrgChart").append(showlist);  
            $("#org").jOrgChart( {  
               chartElement : '#jOrgChart',//指定在某个dom生成jorgchart  
               dragAndDrop : false //设置是否可拖动  
            });  
            $(".node").on('mousedown',function(e){
            	var that = $(this);
            	var orgId = that.find("a").attr("id");
            	if(e.button == 2){
            		$(".orgposition").remove();
            		var operation = '<div class="orgposition"><ul><li class="popedit" method="add">新增</li><li class="popedit" method="edit">修改</li><li class="popremove">删除</li><li class="popSetAdmin">设置管理员</li></ul></div>';
            		$(this).append(operation);
            	} 
            	
            	$(".popedit").on('mousedown',function(ev){
            		ev.stopPropagation();
            		$(".orgposition").remove();
            		var method = $(this).attr("method");
            		if(method == "add"){
            			layer.open({
            		    	  title: '新增子集部门', 
            		  		  skin: 'demo-class',
            		  		  btn: ['确认', '取消'],
            		  		  content: $(".orgform").html(),
            		  		  success: function(layero, index){
            		  			//修改之前执行填充操作
            		  			$(layero).find('#addorganization').validate();
            		  		  },
            		  		  yes:function(index, layero){
            		  			 if($(layero).find('#addorganization').valid()){
            		  			  	console.log($(layero).find('input').val());
	            		  			$.ajax({
	                					url:webroot + '/dep/depSave.do',
	    	            				type:"post",
	    	            				data:{
	    	            					id:orgId,
	    	            					depName:$(layero).find('input').val(),
	    	            					Method:method
	    	            				},
	    	            				success:function(response){
	    	            					layer.msg(response.msg);
	    	            					$("#jOrgChart").html("");
	    	            					startInit();
	    	            				}
	                				});
            		  			  layer.close(index);
            		  			 }
            		  		  },
            		  		  cancel:function(index){
            		  			  layer.close(index);
            		  		  }
            		  		});
            		}else{
            			layer.open({
          		    	  title: '修改部门名称', 
          		  		  skin: 'demo-class',
          		  		  btn: ['确认', '取消'],
          		  		  content: $(".orgform").html(),
          		  		  success: function(layero, index){
          		  			//修改之前执行填充操作
          		  			$(layero).find('input').val(that.find("a").html());
          		  		   $(layero).find('#addorganization').validate();
          		  		  },
          		  		  yes:function(index, layero){
          		  			 if($(layero).find('#addorganization').valid()){
	          		  			$.ajax({
	            					url:webroot + '/dep/depSave.do',
		            				type:"post",
		            				data:{
		            					id:orgId,
		            					depName:$(layero).find('input').val(),
		            					Method:method
		            				},
		            				success:function(response){
		            					layer.msg(response.msg);
		            					$("#jOrgChart").html("");
		            					startInit();
		            				}
	            				});
          		  			    layer.close(index);
          		  			 }
          		  		  },
          		  		  cancel:function(index){
          		  			  layer.close(index);
          		  		  }
          		  		});
            		}
            	});
            	$(".popremove").on('mousedown',function(ev){
            		ev.stopPropagation();
            		
            		layer.confirm('确认删除?', {icon: 3, title:'删除'}, function(index){
            			$.ajax({
        					url:webroot + '/dep/deleteDept.do',
            				type:"post",
            				data:{
            					Did:orgId,
            				},
            				success:function(response){
            					layer.msg(response.msg);
            					if(response.success == false){
            						alert(response.messages[0]);
            						return;
            					}
            					$("#jOrgChart").html("");
            					startInit();
            				}
        				});
            		    layer.close(index);
            		});
        		});
            	
            	
            //  设置管理员
                $(".popSetAdmin").on('click',function(){
                	
                	layer.open({
                	  title: '设置管理员', 
              		  skin: 'demo-class',
              		  btn: ['确认', '取消'],
              		  area:['350px'],
              		  content: $(".setAdminenter").html(),
              		  success: function(layero, index){
            	  			$(layero).find(".leadinput").on('click',function(){
            				    showDevTypeTree($(layero),'treePerlead','leadid','leadname','leadtree',false,false,1)
            			    });
              		  },
              		  yes:function(index, layero){
              			  var leadid=$(layero).find("#leadid").val();
              			  if(leadid==''){
              				layer.msg('请选择管理员');
              				return false;
              			  }else{
              				$.ajax({
            					url : webroot+ "/dep/saveDeptAdmin.ajax",
            					data : {'id':orgId,'leadid':leadid},
            					type : 'POST',
            					success : function(data) {
            						if(data.type == "2"){
            							employeeTable().draw();
            						}
            						layer.msg(data.msg);
            					}
            				});
              				layer.close(index);
              			  }
              			  
            			  
              		  },
              		  cancel:function(index){
              			  layer.close(index);
              		  }
              		});
                });
            	
            });
		}
	})
}
	$(document).on('click',function(){
		$(".orgposition").remove();
	});
});

function showall(menu_list, parent) {  
    $.each(menu_list, function(index, val) {  
        if(val.children.length > 0){  
          var li = $("<li></li>");  
          li.append("<a href='javascript:void(0)' id="+val.id+">"+val.name+"</a>").append("<ul></ul>").appendTo(parent);  
          //递归显示  
          showall(val.children, $(li).children().eq(1));  
        }else{  
          $("<li></li>").append("<a href='javascript:void(0)' id="+val.id+">"+val.name+"</a>").appendTo(parent);  
        }  
    });  

}  
